# coding:utf-8
from ctypes import *
import requests
import json
import random
import binascii
import config
from PIL import Image


class Dama2():
    """打码兔打码."""
    _username = 'test'
    _password = 'test'
    __attrs__ = ['DM', 'username', 'password', 'softuuid', 'timeout']

    def __init__(self):
        self.DM = WinDLL('lib/CrackCaptchaAPI.dll')
        if not self._username:
            Dama2._username = config['dama']['dama2']['username']
            Dama2._password = config['dama']['dama2']['password']
        self.username = c_char_p(self._username)
        self.password = c_char_p(self._password)
        self.softuuid = c_char_p('e17f9c4cc1205f152b81ac15f7aec3e3')
        self.timeout = c_ushort(30)

    def recv_byte(self, imgdata, imgtype=34):
        # imgdata = c_void_p(imgdata)
        imgleng = c_uint(len(imgdata))
        imgtype = c_ulong(imgtype)
        res = c_char_p('')

        code = self.DM.D2Buf(self.softuuid, self.username, self.password, imgdata, imgleng, self.timeout, imgtype, res)
        if code > 0:
            return res.value
        return code

    def report_err(self, imgid):
        return False




class Dama():
    flag = 'dama2'

    def __init__(self):
        if self.flag == 'dama2':
            self.w = Dama2()
        else:
            self.w = Dama2()  # 默认

    def recv_byte(self, imgdata):
        return self.w.recv_byte(imgdata)

    def report_err(self, imgid):
        return self.w.report_err(imgid)


# test
if __name__ == '__main__':
    img=open('my.png','r').read()
    dama=Dama()
    print dama.recv_byte(img)

"""
username and password 更换为自己的
"""